package sword_offer;

/**
 * @author Synhard
 * @version 1.0
 * @Class sword_offer.Code7
 * @Description 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，
 * 分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead操作返回 -1 )
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-03-12 22:56
 */
public class Code7 {
    public static void main(String[] args) {
        CQueue q = new CQueue();
        q.appendTail(1);

        System.out.println(q.deleteHead());
        System.out.println(q.deleteHead());
        System.out.println(q.deleteHead());

    }
}

class CQueue {
    StackForCode7 stack1;
    StackForCode7 stack2;

    public CQueue() {
        this.stack1 = new StackForCode7(10000);
        this.stack2 = new StackForCode7(10000);
    }

    public void appendTail(int value) {
        this.stack1.push(value);
    }

    public int deleteHead() {
        if (this.stack1.p == -1) {
            return -1;
        }
        while (this.stack1.p >= 0) {
            this.stack2.push(this.stack1.pop());
        }
        int res = this.stack2.pop();
        while (this.stack2.p >= 0) {
            this.stack1.push(this.stack2.pop());
        }
        return res;
    }
}

class StackForCode7 {
    int[] val;
    int p;
    StackForCode7 (int capacity) {
        this.val = new int[capacity];
        this.p = -1;
    }
    void push(int val) {
        this.val[++this.p] = val;
    }
    int pop() {
        return this.val[this.p--];
    }
}